 <?php
header('Content-Type: application/json');

require_once("util.php");
require_once("sql.php");

require_once("user.php"); 
require_once("company.php"); 
require_once("unit.php");
require_once("manageunit.php");
require_once("managecompany.php");

	$key ="user";
	$user=null;
	if(session_id() == '') {
		session_start();
	}
	$key = "user";
	$user = unserialize ( $_SESSION [$key] );
	$manageUnit =new ManageUnit();
	$manageCompany =new ManageCompany();
	$unitId=$user->getUnitId();
	$canViewAllCompany=$user->isActionAllowed("CAN_VIEW_ALL_COMPANY","GLOBAL");
	
$user= unserialize($_SESSION[$key]);

if($_SERVER['REQUEST_METHOD'] == "GET"){
	$units=array();
	$responseJson="";
	//Get the fulldetils of company(for display in table)
	if (array_key_exists("fulldetails",$_REQUEST)){
		
		$units=$manageUnit->getUnitList();
		$responseJson .="[";

		if(count($units)==0){
			$unit=$units[0];
			$responseJson .="{";
			$responseJson .= '"id":"' .$unit->getId() .'",';
			$responseJson .= '"shortname":"' .$unit->getUnitShort() .'",';
			$responseJson .= '"unitname":"' .$unit->getUnit() .'"';
			$responseJson .="}";
		}else {
			foreach($units as $unit){
				$responseJson .="{";
				$responseJson .= '"id":"' .$unit->getId() .'",';
				$responseJson .= '"shortname":"' .$unit->getUnitShort() .'",';
				$responseJson .= '"unitname":"' .$unit->getUnit() .'"';
				$responseJson .="},";
			}
			$responseJson=substr($responseJson,0,strlen($responseJson)-1);
		}
		$responseJson .="]"; 
	}else {//for update & delete operation, get the information about the selected unit
		if (array_key_exists("key",$_REQUEST)){
			$unitPK= $_REQUEST["key"];
			$unit=$manageUnit->getUnit($unitPK);
			$id=$unit->getId();
			$unitName=$unit->getUnit();
			$unitShort=$unit->getUnitShort();
			$responseJson='[';
					
			$company=$manageUnit->getCompanyByUnitId($id);
			$companyName ='';
			if($company !=null){
				$companyName =$company->getCompany()==null ?'':$company->getCompany();
			}
			$responseJson .='{ "id":"company_select", "type":"label","label":"Company","display":"true", "value" :"'.$companyName.'"},';
			$responseJson .= '{"id":"unit_name","label":"Unit Name","display":"true","value":"' .$unitName .'","type":"input_text"},{"id":"unit_short","label":"Unit Short Name","display":"true","value":"' .$unitShort .'","type":"input_text"}]';

		}else {//For create operation
			$responseJson ='[';
			if($canViewAllCompany){
					$companyStrs=array();
					$companies=$manageCompany->getCompanyList();
					foreach($companies as $company) {
						$companyStr='{"op_value":"' .$company->getId() .'","op_label": "' .$company->getCompanyShort() .'"}';
						array_push($companyStrs,$companyStr);
					}
					$responseJson .='{ "id":"company_select", "type":"input_select","label":"Company","display":"true", "value" :[' .implode(",",$companyStrs) .']},';
			}else{
				$company=$manageUnit->getCompanyByUnitId($unitId);
				$companyName=$company->getCompany();
				$responseJson .='{ "id":"company_select", "type":"label","label":"Company","display":"true", "value" :"'.$companyName.'"},';
			}
			$responseJson .='{"id":"unit_name","label":"Unit Name","display":"true","type":"input_text"},{"id":"unit_short","label":"Unit Short Name","display":"true","type":"input_text"}]';
		}
	}
	echo $responseJson;
}elseif ($_SERVER['REQUEST_METHOD'] == "POST"){
	if (array_key_exists("action",$_REQUEST)){
		$action= $_REQUEST["action"];
		
		$success ='{"status" :"success"}';
		$failure ='{"status" :"faiure"}';
		if($action=="create"){
			$status=$manageUnit->addUnit($_REQUEST["company_select"],$_REQUEST["unit_name"],$_REQUEST["unit_short"]);
			if($status){echo $success;}
			else {echo $failure;}
		}elseif($action=="update"){
			$status=$manageUnit->updateUnit($_REQUEST["key"],$_REQUEST["unit_name"],$_REQUEST["unit_short"]);
			if($status){echo $success;}
			else {echo $failure;}
		}else if($action=="delete"){
			$status=$manageUnit->deleteUnit($_REQUEST["key"]);
			if($status){echo $success;}
			else {echo $failure;}
		}else{
			echo $failure;
		}
	}else{
		echo $failure;
	}
}
?>